package com.et.u1

import android.content.Context
import android.net.Uri
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.et.core.goods.GoodsInfo
import com.et.core.goods.GoodsProvider
import com.et.core.util.AdapterFoot
import com.et.u1.databinding.ItemShopListCardBinding
import com.et.u1.databinding.ItemShopListFootBinding
import com.et.u1.util.discountTypeFace
import com.et.u1.util.size

class MainModel : ViewModel() {

    val replaceLiveData = MutableLiveData<Fragment>()


    var onClickGoodsListener: (() -> Unit)? = null

    var purchasedGoodsInfo: GoodsInfo? = null
        private set

    private val callFoot: (binding: ViewBinding) -> Unit = { v ->
        if (v is ItemShopListFootBinding) {

        }
    }
    private val callGoods: (binding: ViewBinding, t: GoodsInfo) -> Unit = { v, p ->
        if (v is ItemShopListCardBinding) {
            if (p.discount != 100) {
                v.discountCard.visibility = View.VISIBLE
                v.discount.run {
                    text = String.format("%.1f折", p.discount / 10f)
                    typeface = discountTypeFace
                }
                v.balance.text = String.format("￥%.2f", (p.channel.price * p.discount / 10000f))
            } else {
                v.discountCard.visibility = View.GONE
                v.balance.text = String.format("￥%.2f", (p.channel.price / 100f))
            }
            v.name.text = p.name
            v.image.setImageURI(Uri.parse(p.image))
//            if (p.channel.nu == 0) {
//                v.soldOut.visibility = View.VISIBLE
//                v.root.alpha = 0.56f
//            }
            v.root.setOnClickListener {
                purchasedGoodsInfo = p
                onClickGoodsListener?.let { it() }
            }
        }
    }

    private val bindingFoot: (inflater: LayoutInflater, parent: ViewGroup) -> ItemShopListFootBinding =
        { inflater, parent ->
            ItemShopListFootBinding.inflate(inflater, parent, false)
        }
    private val bindingGoods: (inflater: LayoutInflater, parent: ViewGroup) -> ItemShopListCardBinding =
        { inflater, parent ->
            ItemShopListCardBinding.inflate(inflater, parent, false)
        }

    private val adapter: AdapterFoot<GoodsInfo> by lazy {
        AdapterFoot(
            callFoot = callFoot,
            callGoods = callGoods,
            bindingFoot = bindingFoot,
            bindingGoods = bindingGoods
        )
    }

    fun bindGoodsAdapter(
        recyclerview: RecyclerView,
        layoutManager: GridLayoutManager,
        _callback: () -> Unit
    ) {
        recyclerview.adapter = adapter
        onClickGoodsListener = _callback
        layoutManager.spanSizeLookup = object : SpanSizeLookup() {
            override fun getSpanSize(position: Int): Int {
                return if (position == adapter.itemCount - 1) layoutManager.spanCount else 1
            }
        }
        recyclerview.layoutParams.width = size.x - size.x % 194
        recyclerview.layoutManager = layoutManager
        recyclerview.setHasFixedSize(true)
        recyclerview.isNestedScrollingEnabled = false
    }

    fun initGoodsProvider(context: Context) {
        GoodsProvider.getInstance().apply {
            registerContentObserver(context)
            observer { r, dest -> adapter.dispatchUpdate(r, dest) }
            initShowList(context)
        }
    }
}